<div class="row" ng-controller="addContent">
    <div class="col-xs-12">
        <div class="box box-default">
            <div class="box-header">
                <div class="alert alert-success alert-dismissable fade in hide" role="alert" id="addSuccess">
                    <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                    <p><i class="icon fa fa-check"></i> <strong>恭喜！</strong>内容添加成功</p>
                </div>
                <p class="myInfoBox bg-warning text-warning"><i class="icon fa fa-warning"></i> 信息填写完整无误才可提交</p>
            </div>
            <div class="box-body">
                <form role="form" class="form-horizontal" name="myForm" ng-submit="processForm(myForm.$valid)" novalidate>
                    <div class="form-group">
                        <label class="control-label col-sm-3">文章标题</label>
                        <div class="col-sm-5">
                            <input type="text" class="form-control input-sm" name="title" ng-minlength="5" ng-maxlength="50" ng-model="formData.title" required/>
                            <label for="inputError" class="control-label text-danger" ng-show="myForm.title.$invalid && !myForm.title.$pristine"><i class="fa fa-times-circle-o"></i> 5-50个非特殊字符</label>

                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-3">简短标题</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control input-sm" name="stitle" ng-minlength="5" ng-maxlength="40" ng-model="formData.stitle" required/>
                            <label for="inputError" class="control-label text-danger" ng-show="myForm.stitle.$invalid && !myForm.stitle.$pristine"><i class="fa fa-times-circle-o"></i> 5-40个非特殊字符</label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-3">来源</label>
                        <div class="radio col-sm-4">
                            <label>
                                <input type="radio" checked class="mini" value="1" name="from" ng-model="formData.from"/>原创&nbsp;
                            </label>
                            <label>
                                <input type="radio" class="mini" name="from" value="2" ng-model="formData.from"/>转载
                            </label>
                        </div>
                    </div>
                    <div class="form-group hide">
                        <label class="control-label col-sm-3">发布</label>
                        <div class="checkbox col-sm-4">
                            <label>
                                <input type="checkbox" checked class="mini" name="state" ng-model="formData.state"/>是
                            </label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-3">TAG标签</label>
                        <div class="col-sm-4">
                            <div class="input-group margin">
                                <input type="text" class="form-control" name="tags" id="tagSel" ng-minlength="2" ng-maxlength="27" ng-model="formData.tags" onclick="showTagsMenu()" required/>

                                <div class="input-group-btn">
                                    <button type="button" class="btn btn-default dropdown-toggle"  id="menuBtn" onclick="showTagsMenu(); return false;">选择 <span class="fa fa-caret-down"></span></button>
                                    <div id="menuContent" class="menuContent" style="display:none; position: absolute;border: 1px solid #ccc;border-radius: 5px;left: -14px;top: 43px;z-index: 999">
                                        <ul class="ztree" id="tagsTree" style="margin-top: 0;width: 180px;max-height: 200px;overflow-y: auto;padding: 10px;background: #fff;"></ul>
                                    </div>
                                </div><!-- /btn-group -->
                            </div><!-- /input-group -->
                            <label for="inputError" class="control-label text-danger" ng-show="myForm.tags.$invalid && !myForm.tags.$pristine"><i class="fa fa-times-circle-o"></i>标签用逗号隔开，单个标签不可超过6个字，不得超过4个标签</label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-3">缩略图</label>
                        <div class="col-sm-3">
                            <input type="file" name="uploadify" id="uploadContentImg" />
                            <img src="" alt="" width="120" height="120" class="img-thumbnail" id="myImg">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-3">文章类别</label>
                        <div class="col-sm-2">
                            <div class="dropdown">
                                <button id="dLabel" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                    <i id="categoryName">请选择文章类别</i>
                                    <span class="caret"></span>
                                </button>
                                <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel" id="cateTree" class="ztree">
                                    <li role="presentation"><a href="#">test</a></li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-3">关键字</label>
                        <div class="col-sm-5">
                            <input type="text" class="form-control input-sm" name="keywords" ng-minlength="2" ng-maxlength="60" ng-model="formData.keywords" required/>
                            <label for="inputError" class="control-label text-danger" ng-show="myForm.keywords.$invalid && !myForm.keywords.$pristine"><i class="fa fa-times-circle-o"></i>60个字以内，多个关键字之间用逗号隔开</label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-3">内容摘要</label>
                        <div class="col-sm-6">
                            <textarea class="form-control input-sm" name="discription" ng-model="formData.discription" ng-minlength="15" ng-maxlength="140" required id="" cols="30" rows="6"></textarea>
                            <label for="inputError" class="control-label text-danger" ng-show="myForm.discription.$invalid && !myForm.discription.$pristine"><i class="fa fa-times-circle-o"></i> 15-140个非特殊字符</label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-3">文章详情</label>
                        <div class="col-sm-8">
                            <textarea class="" name="comments" ng-model="formData.comments" id="container" ng-minlength="50" placeholder="请输入文章详情" ueditor>{{formData.comments}}</textarea>
                            <label for="inputError" class="control-label text-danger" ng-show="myForm.comments.$invalid && !myForm.comments.$pristine"><i class="fa fa-times-circle-o"></i>内容不得少于50个字</label>

                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" ng-show="getContentState()" ng-click="saveAsDraft()">存为草稿</button>
                        <button type="submit" class="btn btn-primary" ng-disabled="myForm.$invalid">发布</button>
                    </div>
                </form>

            </div>
        </div>
    </div>
</div>

<script>

            doraApp.controller("addContent",function($scope,$http){
                $scope.formData = {};
                $scope.formData.author = '<%=adminUserInfo.name%>';

//                初始化文章分类
                iniTreeData($scope,$http);
//                初始化文章标签
                initContentTags($scope,$http);
//                初始化上传按钮
                initUploadFyBtn('uploadContentImg',"ctTopImg",function(data){
				    alert('上传成功');
                    $("#myImg").attr("src",data);
                    $scope.formData.sImg = data;
                });
//                通过访问地址获取文章id
                $scope.targetID = window.location.href.split("/")[8];
                if($scope.targetID){
                    $http.get("/admin/manage/<%=bigCategory%>/item?uid="+$scope.targetID).success(function(result){
                        $scope.formData = result;
                        iniTreeData($scope,$http);
                        initContentTags($scope,$http);
                        $("#myImg").attr("src",$scope.formData.sImg)
                    })
                }
                // 添加或修改文章
                $scope.processForm = function(isValid){
                    $scope.formData.state = true;
                    if(!$scope.formData.category){
                        alert('请选择文档类别');
                        return false;
                    }
                    angularHttp($http,isValid,'POST',getTargetPostUrl($scope,'<%=bigCategory%>'),$scope.formData,function(data){
                        window.location = "/admin/manage/contentList";
                    });
                };
//                存草稿
                $scope.saveAsDraft = function(){
                    $scope.formData.state = false;
                    if(!$scope.formData.title){
                        alert('文档标题必须填写');
                        return false;
                    }
                    if(!$scope.formData.category){
                        alert('文档类别必须选择');
                        return false;
                    }
                    angularHttp($http,true,'POST',getTargetPostUrl($scope,'<%=bigCategory%>'),$scope.formData,function(data){
                        window.location = "/admin/manage/contentList";
                    });
                };

                $scope.getContentState = function(){
                    if(!$scope.formData.state && $scope.targetID){
                        return true;
                    }else if($scope.targetID == undefined){
                        return true;
                    }else{
                        return false;
                    }
                }
            })
            .directive('ueditor', function ($timeout) { //angular绑定ueditor
                return {
                    restrict: 'A',
                    require: 'ngModel',
                    link: function (scope, element, attrs, ctrl) {
                        var id = 'ueditor_' + Date.now();
                        element[0].id = id;
                        var ue = UE.getEditor(id, {
                            initialFrameWidth: '100%',
                            initialFrameHeight: '500',
                            autoHeightEnabled: true
                        });
                        ue.ready(function () {
                            ue.addListener('contentChange', function () {
                                ctrl.$setViewValue(ue.getContent());
                                if (!scope.$$phase) {
                                    scope.$apply();
                                }
                            });
                        });
                    }
                };
            });

           function iniTreeData($scope,$http){

               var currentCate = '';
               if($scope.formData){
                   currentCate = $scope.formData.category;
               }

               iniNormalTree($http,'cateTree',"/admin/manage/contentCategorys/list",'categoryName',currentCate,function(e, treeId, treeNode){
                   var zTree = $.fn.zTree.getZTreeObj("cateTree"),
                           nodes = zTree.getSelectedNodes(),
                           v = "",
                           vid = "",
                           sortPath = "",
                           contentTemp = "";

                   nodes.sort(function compare(a,b){return a.id-b.id;});
                   for (var i=0, l=nodes.length; i<l; i++) {
                       v += nodes[i].name ;
                       vid += nodes[i].id ;
                       sortPath += nodes[i].sortPath ;
                       contentTemp += nodes[i].contentTemp ;
                   }
                   $('#categoryName').html(v);
                   $scope.formData.category = vid;
                   $scope.formData.sortPath = sortPath;
//                       设置文章模板
                   $scope.formData.contentTemp = contentTemp;
//                        针对顶级类别的文章做标记
                   if(sortPath.split(',').length == 2){
                       $scope.formData.type = "singer";
                   }else if(sortPath.split(',').length == 3){
                       $scope.formData.type = "content";
                   }
               });

           }


            function initContentTags($scope,$http){
                $http.get("/admin/manage/contentTags/list").success(function(result){
                    var oldTags = $scope.formData.tags;
                    if(!oldTags){
                        oldTags = "0";
                    }
                    var tagsTree = changeToTreeJson(result,"tags",oldTags);
                    var setting = {
                        check: {
                            enable: true,
                            chkboxType: {"Y":"", "N":""}
                        },
                        view: {
                            dblClickExpand: false
                        },
                        data: {
                            simpleData: {
                                enable: true
                            }
                        },
                        callback: {
                            beforeClick: beforeTagsClick,
                            onCheck: onTagsCheck
                        }
                    };

                    var zNodes = tagsTree;
                    $.fn.zTree.init($("#tagsTree"), setting, zNodes);

                    function beforeTagsClick(treeId, treeNode) {
                        var zTree = $.fn.zTree.getZTreeObj("tagsTree");
                        zTree.checkNode(treeNode, !treeNode.checked, null, true);
                        return false;
                    }

                    function onTagsCheck(e, treeId, treeNode) {
                        var zTree = $.fn.zTree.getZTreeObj("tagsTree"),
                                nodes = zTree.getCheckedNodes(true),
                                v = "";
                        for (var i=0, l=nodes.length; i<l; i++) {
                            if(i > 3){
                                return;
                            }
                            v += nodes[i].name + ",";
                        }
                        if (v.length > 0 ) {
                            v = v.substring(0, v.length-1);
                        }
                        var tagObj = $("#tagSel");
                        tagObj.val("");
                        tagObj.val(v);
                        tagObj.attr("value", v);
                        $scope.formData.tags = v;
                    }
                })
            }

            function showTagsMenu() {
                var cityObj = $("#tagSel");
                var cityOffset = $("#tagSel").offset();
                $("#menuContent").slideDown("fast");

                $("body").bind("mousedown", onBodyDown);
            }
            function hideTagsMenu() {
                $("#menuContent").fadeOut("fast");
                $("body").unbind("mousedown", onBodyDown);
            }
            function onBodyDown(event) {
                if (!(event.target.id == "menuBtn" || event.target.id == "tagSel" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
                    hideTagsMenu();
                }
            }

</script>